package chonggou;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {
	private String name;
	private Vector<Rental> rentals = new Vector<Rental>();

	public Customer(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Vector<Rental> getRentals() {
		return rentals;
	}

	public void setRentals(Vector<Rental> rentals) {
		this.rentals = rentals;
	}

	public void addRental(Rental rental) {
		rentals.addElement(rental);
	}

	/**
	 * 生成详单的函数
	 * 
	 * @return
	 */
	public String statement() {
		Enumeration<Rental> enumeration = rentals.elements();
		String result = "Rental Record for " + getName() + "\n";

		while (enumeration.hasMoreElements()) {
			Rental rental = enumeration.nextElement();

			result += "\t" + rental.getMovie().getTitle() + "\t"
					+ String.valueOf(rental.getCharge()) + "\n";

		}

		result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
		result += "You earned " + String.valueOf(getTotalFrequentRenterPoints())
				+ " frequent renter points";
		return result;
	}

	private int getTotalFrequentRenterPoints() {
		int result = 0;
		Enumeration<Rental> enumeration = rentals.elements();
		while (enumeration.hasMoreElements()) {
			Rental rental = enumeration.nextElement();
			result += rental.getFrequentRenterPoints();
		}
		return result;
	}

	private double getTotalCharge() {
		double result = 0;
		Enumeration<Rental> enumeration = rentals.elements();
		while (enumeration.hasMoreElements()) {
			Rental rental = enumeration.nextElement();
			result += rental.getCharge();
		}
		return result;
	}


}
